/*******************************************************************************
 * Package: com.song.boot.springstudy.webflux
 * Type:    CollectMain
 * Date:    2025-04-06 16:52
 *
 * Copyright (c) 2025 LTD All Rights Reserved.
 *
 * You may not use this file except in compliance with the License.
 *******************************************************************************/
package com.song.boot.springstudy.webflux;

import reactor.core.publisher.Flux;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 功能描述：   收集响应式流
 *
 * @author Songxianyang
 * @date 2025-04-06 16:52
 */
public class CollectMain {
    public static void main(String[] args) {
        Flux.just(1,4,55,66,22,77,88).collectList().subscribe(System.out::println);
        // 默认升序
        Flux.just(1,4,55,66,22,77,88).collectSortedList().subscribe(System.out::println);

        Flux.just(1,4,55,66,22,77,88).collectMap(v->"value"+v).subscribe(System.out::println);

        Flux.just(1,4,55,66,22,77,88).collectMap(
                k-> k,
                v-> v+"value"
        ).subscribe(System.out::println);



        Flux.just(1,4,55,66,22,77,88).collectMap(
                k-> k,
                v-> v+"value",
                // 往当前集合加数据
                ()-> {
                    Map<Integer,String> map = new HashMap<>();
                    map.put(888, "测试");
                    return map;
                }
        ).subscribe(System.out::println);

        Flux.just(1,4,55,66,22,77,88).collectMultimap(
                k->k,
                v-> {
                    List list = new ArrayList();
                    list.add(v);
                    list.add("lllll");
                    return list;
                }
        ).subscribe(System.out::println);


        Flux.just(1,2)
                // 重复
                .repeat(3)// 实际上是打印四次，一次原始的，三次重复的
                .subscribe(System.out::print);

        Flux.empty().defaultIfEmpty("hello defaultIfEmpty").subscribe(System.out::println);


        Flux.just(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2)
                .distinctUntilChanged()
                .subscribe(System.out::print);
        System.out.println("===============");
        Flux.just(1,1,1,2,2,2,3,3,3,1,1,1,2,2,2)
                .distinct()
                .subscribe(System.out::print);


    }
}
